package com.fudan.backend.controller;

import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.DeliveryInfo;
import com.fudan.backend.service.DeliveryService;
import com.fudan.backend.util.JwtUtil;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/delivery")
public class DeliveryController {
    @Autowired
    DeliveryService deliveryService;
    @GetMapping("/info/{storeId}")
    public R getDeliveryInfo(Integer current, Integer size, @PathVariable("storeId") String storeId, @RequestHeader String token){
        String userId = JwtUtil.getUserId(token);
        PageInfo<DeliveryInfo> infoPage = deliveryService.getDeliveryInfoByStoreId(storeId, userId, current, size);
        Map<String, Object> map = new HashMap<>();
        map.put("deliveryInfoList", infoPage.getList());
        map.put("total", infoPage.getTotal());
        return new R(RetCode.DELIVERY_INFO_SUCCESS, map);
    }
    @PostMapping("/process")
    public R setDeliveryInfoStatus(@RequestBody Map<String, Object> map, @RequestHeader String token){
        String userId = JwtUtil.getUserId(token);
        String deliveryInfoId;
        try{
            deliveryInfoId = map.get("deliveryInfoId").toString();
        }
        catch (Exception e){
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        RetCode retCode = deliveryService.setDeliveryInfoShip(deliveryInfoId, userId);
        return new R(retCode);
    }
}
